package com.balance.anole.erp.report.view;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import com.balance.anole.business.vo.OrderProductItemTopTO;
import com.balance.anole.erp.report.util.IReportID;
import com.jasperassistant.designer.viewer.ViewerComposite;

public class ReportDemoPreView extends ViewPart{
	public static final String ID = "com.balance.anole.erp.ReportDemoPreView";
	
	@Override
	public void createPartControl(Composite parent) {
		ViewerComposite viewer = new ViewerComposite(parent, SWT.NONE);
		
		String secondaryId = getViewSite().getSecondaryId();
		List dataList = new ArrayList();
		Map reportParam = new HashMap();
		
		if(StringUtils.isEmpty(secondaryId)){
			secondaryId = IReportID.RPT_DEMO;
		}
		
		if(IReportID.RPT_ORDER_001.equals(secondaryId)){	
			OrderProductItemTopTO o1 = new OrderProductItemTopTO();
			o1.setColorCd("R");
			o1.setColorDesc("Red");
			o1.setOrderNum(100);
			o1.setProductDesc("cloths");
			o1.setProductId("1");
			
			OrderProductItemTopTO o2 = new OrderProductItemTopTO();
			o2.setColorCd("B");
			o2.setColorDesc("Blue");
			o2.setOrderNum(200);
			o2.setProductDesc("T-Shirt1");
			o2.setProductId("2");
			
			OrderProductItemTopTO o3 = new OrderProductItemTopTO();
			o3.setColorCd("B");
			o3.setColorDesc("Blue");
			o3.setOrderNum(200);
			o3.setProductDesc("T-Shirt2");
			o3.setProductId("3");
			
			OrderProductItemTopTO o4 = new OrderProductItemTopTO();
			o4.setColorCd("B");
			o4.setColorDesc("Blue");
			o4.setOrderNum(200);
			o4.setProductDesc("T-Shirt3");
			o4.setProductId("4");
			
			OrderProductItemTopTO o5 = new OrderProductItemTopTO();
			o5.setColorCd("B");
			o5.setColorDesc("Blue");
			o5.setOrderNum(200);
			o5.setProductDesc("T-Shirt4");
			o5.setProductId("5");
			
			OrderProductItemTopTO o6 = new OrderProductItemTopTO();
			o6.setColorCd("B");
			o6.setColorDesc("Blue");
			o6.setOrderNum(200);
			o6.setProductDesc("T-Shirt5");
			o6.setProductId("6");
			
			dataList.add(o1);
			dataList.add(o2);
			dataList.add(o3);
			dataList.add(o4);
			dataList.add(o5);
			dataList.add(o6);
			
			
			List<String[]> paramForChart = new ArrayList<String[]>();
			for(int i=0;i<dataList.size();i++){
				String prodDesc = ((OrderProductItemTopTO)dataList.get(i)).getProductDesc();
				String colorDesc = ((OrderProductItemTopTO)dataList.get(i)).getColorDesc();
				int orderNum = ((OrderProductItemTopTO)dataList.get(i)).getOrderNum();
				
				String[] chartDesc = new String[2];
				chartDesc[0] = prodDesc + "[" + colorDesc + "]";
				chartDesc[1] = orderNum+"";
				
				paramForChart.add(chartDesc);
			}	
			
			reportParam.put("paramForChart", paramForChart);
		}
		

		
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dataList);
		
		try {
			InputStream io = this.getClass().getClassLoader().getResourceAsStream(secondaryId + ".jasper");   
			JasperPrint s = JasperFillManager.fillReport(io, reportParam, dataSource);
			viewer.getReportViewer().setDocument(s);	
		} catch (JRException e) {		
			e.printStackTrace();
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}
}
